package com.scrum.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.scrum.repository.IUserRepository;
import com.scrum.vos.Confirmation;
import com.scrum.vos.ConfirmationState;
import com.scrum.vos.User;

@Service
@Transactional
public class UserServices implements IUserServices{
	
	@Autowired
	IUserRepository userRepository;
	
	@Override
	public Confirmation createUser(User user) throws Exception {
		
		Confirmation confirmation = new Confirmation();
		
		if(!this.validateCompleteData(user))
		{
			confirmation.setConfirmationState(ConfirmationState.ERROR);
			return confirmation;
		}
		
		if(!this.validateExistUser(user))
		{
			confirmation.setConfirmationState(ConfirmationState.ERROR);
			return confirmation;
		}
		
		userRepository.saveUpdate(user);
		confirmation.setConfirmationState(ConfirmationState.OK);
		return confirmation;
	}

	@Override
	public Confirmation deleteUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Confirmation modifyUser(User user) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public User login(String nickName, String password) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Confirmation logout(String nickName, String password) {
		// TODO Auto-generated method stub
		return null;
	}
	
	private boolean validateExistUser(User user)
	{
		if(userRepository.findUserByAttribute("mail", user.getMail()) != null){
			return false;
		}
		
		if(userRepository.findUserByAttribute("nickName", user.getNickName()) != null){
			return false;
		}
		
		return true;
	}
	
	private boolean validateCompleteData(User user)
	{
		if(user.getMail() == "" || user.getMail() == null)
		{
			return false;
		}
		
		if(user.getName() == "" || user.getName() == null)
		{
			return false;
		}
		
		if(user.getNickName() == "" || user.getNickName() == null)
		{
			return false;
		}
		
		if(user.getPassword() == "" || user.getPassword() == null)
		{
			return false;
		}
		
		if(user.getSurName() == "" || user.getSurName() == null)
		{
			return false;
		}		
		
		return true;
	}

	@Override
	public List<User> getListUser() throws Exception {
		// TODO Auto-generated method stub
		return userRepository.getListEntity(User.class);
	}
	

}
